package com.hsbc.dev.teamo4.sms.sm.service.impl;

import com.hsbc.dev.teamo4.sms.sm.dao.IAccountDao;
import com.hsbc.dev.teamo4.sms.sm.dao.IUserDao;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.AccountDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.UserDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.po.Account;
import com.hsbc.dev.teamo4.sms.sm.po.User;
import com.hsbc.dev.teamo4.sms.sm.service.IAccountService;

import java.util.List;

/**
 * @author:Cady
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.service.impl
 * @Description:
 * @Date date: 2018-07-27
 */
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao = new AccountDaoImpl();
    private IUserDao userDao = new UserDaoImpl();

    @Override
    public Account save(Account account) {
        if(account.getUsername()==null|| "".equals(account.getUsername().trim())) {
            return null;
        }
        if (account.getCreateTime()==null) {
            account.setCreateTime("" + System.currentTimeMillis());
        }
        return accountDao.saveEntity(account);
    }

    @Override
    public Account update(Account account) {
        if (account.getId()==null||account.getId().trim().equals("")) {
            return null;
        }
        if (account.getUpdateTime()==null) {
            account.setUpdateTime("" + System.currentTimeMillis());
        }
        return accountDao.updateEntity(account);
    }

    @Override
    public Account queryById(String id) {
        return accountDao.queryById(id);
    }

    @Override
    public List<Account> queryEntityList() {
        return accountDao.queryAllEntity();
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        return accountDao.updateByIsEnable(id,bool);
    }

    @Override
    public boolean loginCheck(Account account){
        if(account==null||account.getUsername()==null|| "".equals(account.getUsername().trim()) ||account.getPassword()==null|| "".equals(account.getPassword().trim())) {
            return false;
        }
        Account resultAccount = accountDao.queryById(account.getId());
        if(resultAccount!=null&&resultAccount.getPassword().equals(account.getPassword())){
            User user = userDao.queryByAccountId(account.getId());
            if (user==null){
                return false;
            }
            return user.getIsEnable();
        }
        return false;
    }
}
